package com.example.myfirstapp;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
//import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {
	Handler myHandler;
	Ground[][] myMapa;
	ImageView tempImage;
	Robot myRobot;
	

    final MainActivity self = this;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(new GameView(this));
        setContentView(R.layout.activity_main);
        myHandler = new Handler();
        //myHandler.initializeMap(this);
        myHandler.createMap1(this);
        myHandler.loadMap(Constants.Mapa1, this);
        UpdateMap();
        Button button = (Button) findViewById(R.id.button1);
        ImageButton up = (ImageButton) findViewById(R.id.arrow_up);
        ImageButton down = (ImageButton) findViewById(R.id.arrow_down);
        ImageButton left = (ImageButton) findViewById(R.id.arrow_left);
        ImageButton right = (ImageButton) findViewById(R.id.arrow_right);
        final TextView errorViewer = (TextView) findViewById(R.id.errorMessages);
        errorViewer.setMovementMethod(ScrollingMovementMethod.getInstance());
                
        button.setOnClickListener(new OnClickListener() {			
			public void onClick(View v) {
				myHandler.initializeMap(self);
				UpdateMap();
			}
		});
        
        up.setOnClickListener(new OnClickListener() {			
			public void onClick(View v) {
				errorViewer.setText(myHandler.north(1));
				if (myHandler.isGameWon()) OnWin();
				else 
				UpdateMap();
			}
		});
        
        down.setOnClickListener(new OnClickListener() {			
			public void onClick(View v) {
				errorViewer.setText(myHandler.south(1));
				if (myHandler.isGameWon()) OnWin();
				else 
				UpdateMap();
			}
		});
        
        left.setOnClickListener(new OnClickListener() {			
			public void onClick(View v) {
				errorViewer.setText(myHandler.west(1));
				if (myHandler.isGameWon()) OnWin();
				else 
				UpdateMap();
			}
		});
        
        right.setOnClickListener(new OnClickListener() {			
			public void onClick(View v) {
				errorViewer.setText(myHandler.east(1));
				if (myHandler.isGameWon()) OnWin();
				else 
				UpdateMap();
			}
		});
    }
    
    public void OnWin()
    {
    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	builder.setMessage("Felicidades! Acaba de ganar!")
    	       .setCancelable(false)
    	       .setPositiveButton("Genial!", new DialogInterface.OnClickListener() {
    	           public void onClick(DialogInterface dialog, int id) {
    	        	   self.finish();
    	           }
    	       });
    	AlertDialog alert = builder.create();
    	alert.show();
    }

    public boolean UpdateMap()
    {
    	myMapa = myHandler.getMapa();
    	myRobot = myHandler.getRobot();
    	 ((ImageView)findViewById(R.id.imageView11)).setImageResource(getImageVie(myMapa[0][0]));
    	 ((ImageView)findViewById(R.id.imageView12)).setImageResource(getImageVie(myMapa[1][0]));
    	 ((ImageView)findViewById(R.id.imageView13)).setImageResource(getImageVie(myMapa[2][0]));
    	 ((ImageView)findViewById(R.id.imageView14)).setImageResource(getImageVie(myMapa[3][0]));
    	 ((ImageView)findViewById(R.id.imageView15)).setImageResource(getImageVie(myMapa[4][0]));
    	 ((ImageView)findViewById(R.id.imageView16)).setImageResource(getImageVie(myMapa[5][0]));
    	 ((ImageView)findViewById(R.id.imageView21)).setImageResource(getImageVie(myMapa[0][1]));
    	 ((ImageView)findViewById(R.id.imageView22)).setImageResource(getImageVie(myMapa[1][1]));
    	 ((ImageView)findViewById(R.id.imageView23)).setImageResource(getImageVie(myMapa[2][1]));
    	 ((ImageView)findViewById(R.id.imageView24)).setImageResource(getImageVie(myMapa[3][1]));
    	 ((ImageView)findViewById(R.id.imageView25)).setImageResource(getImageVie(myMapa[4][1]));
    	 ((ImageView)findViewById(R.id.imageView26)).setImageResource(getImageVie(myMapa[5][1]));
    	 ((ImageView)findViewById(R.id.imageView31)).setImageResource(getImageVie(myMapa[0][2]));
    	 ((ImageView)findViewById(R.id.imageView32)).setImageResource(getImageVie(myMapa[1][2]));
    	 ((ImageView)findViewById(R.id.imageView33)).setImageResource(getImageVie(myMapa[2][2]));
    	 ((ImageView)findViewById(R.id.imageView34)).setImageResource(getImageVie(myMapa[3][2]));
    	 ((ImageView)findViewById(R.id.imageView35)).setImageResource(getImageVie(myMapa[4][2]));
    	 ((ImageView)findViewById(R.id.imageView36)).setImageResource(getImageVie(myMapa[5][2]));
    	 ((ImageView)findViewById(R.id.imageView41)).setImageResource(getImageVie(myMapa[0][3]));
    	 ((ImageView)findViewById(R.id.imageView42)).setImageResource(getImageVie(myMapa[1][3]));
    	 ((ImageView)findViewById(R.id.imageView43)).setImageResource(getImageVie(myMapa[2][3]));
    	 ((ImageView)findViewById(R.id.imageView44)).setImageResource(getImageVie(myMapa[3][3]));
    	 ((ImageView)findViewById(R.id.imageView45)).setImageResource(getImageVie(myMapa[4][3]));
    	 ((ImageView)findViewById(R.id.imageView46)).setImageResource(getImageVie(myMapa[5][3]));
    	 ((ImageView)findViewById(R.id.imageView51)).setImageResource(getImageVie(myMapa[0][4]));
    	 ((ImageView)findViewById(R.id.imageView52)).setImageResource(getImageVie(myMapa[1][4]));
    	 ((ImageView)findViewById(R.id.imageView53)).setImageResource(getImageVie(myMapa[2][4]));
    	 ((ImageView)findViewById(R.id.imageView54)).setImageResource(getImageVie(myMapa[3][4]));
    	 ((ImageView)findViewById(R.id.imageView55)).setImageResource(getImageVie(myMapa[4][4]));
    	 ((ImageView)findViewById(R.id.imageView56)).setImageResource(getImageVie(myMapa[5][4]));
    	 
    	 return true;
    }
    
    public int getImageVie(Ground myGround){
    	if (myGround.isClear())
    		return R.drawable.baldoza;
    	if (myGround.isObstacle())
    		return R.drawable.obstacle;
    	if (myGround.isBeacon())
    		return R.drawable.beacon;
    	if (myGround.isBlack())
    		return R.drawable.baldoza_negra;
    	if (myGround.isWhite())
    		return R.drawable.baldoza_blanca;
    	if (myGround.hasRobot()){
    		switch (myRobot.getOrientation()) {
    			case Constants.NORTH: return R.drawable.car_north;
    			case Constants.WEST: return R.drawable.car_west;
    			case Constants.SOUTH: return R.drawable.car_south;
    			case Constants.EAST: return R.drawable.car_east;
    		}
    	}
    	return (R.drawable.arrow_down);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    } 
}
